class  navigation {
  constructor(obj){
     this.nav = $('nav')[0]
     this.obj= obj;
     
     this.render();
     let ul = $('nav ul')[0];
    
     let bool = false ;
     ul.addEventListener('click',(e)=>{
           
          if( this.checkMenu(e,bool)){
             bool = true;
           }else{
            bool =false ;
           } 
     });  
    
  }
checkMenu(e,bool){
   if(!$(e.target).hasClass('hasTwo')){
     for(let i=0; i<$('.hasTwo').length;i++)
     $('.hasTwo')[i].children[i].style.display='none';
    return false;
   }
    if(bool){
 
    e.target.children[0].style.display='none';
    return false;
    } else {
    e.target.children[0].style.display='block';
    return true; 
}
}

  render(){
     
     let ul = document.createElement('ul');
     ul.className = 'nav_main';
    
      for(let i=0;i<this.obj.length;i++){
        let li =document.createElement('li');
              if(this.obj[i].list.length<1){
                
                let a  =document.createElement('a');
                a.innerHTML = this.obj[i].menu;
                li.className = 'nav_item';
                a.href =this.obj[i].herf[0];
                li.insertAdjacentElement('beforeend',a);
              }  else if(this.obj[i].list.length>1)
              {
                 
                li.innerHTML = this.obj[i].menu;
                li.className = 'nav_item';
                let ulTwo = document.createElement('ul');
                ulTwo.className = 'two_menu'
                // ulTwo = document.createElement('ul');
                for(let j=0; j<this.obj[i].list.length;j++){
                let liTwo =document.createElement('li');
                let aTwo =document.createElement('a');
                aTwo.innerHTML = this.obj[i].list[j];
                aTwo.href = this.obj[i].herf[j];
                liTwo.insertAdjacentElement('beforeend',aTwo);
                ulTwo.insertAdjacentElement('beforeend',liTwo)
                }
                li.insertAdjacentElement('beforeend',ulTwo);
                li.classList.add('hasTwo');
                ulTwo.style.display = 'none';               
         }
         ul.insertAdjacentElement('beforeend',li)
         
    }
    this.nav.append(ul);
    
  }
}
let axiosget = axios.create({   
  baseURL: 'http://hvszny.natappfree.cc/test/sendborrowapplication',
  timeout: 3000,
  method:'post'
});
// new navigation(obj);
let obj = [
  {
    menu:'首页',
    list:[],
    herf:['../wjq/index.html']

  },
  {
    menu:'我的',
    list:[],
    herf:['../wjq/index.html#my']

  },
  {
    menu:'热榜',
    list:[],
    herf:['../wjq/index.html#hot-list']


  },
 
  {
    menu:'图书库',
    list:[],
    herf:['../wjq/index.html#book-list']


  },
  {
    menu:'用户反馈',
    list:[],
    herf:['../wjq/index.html#user-message']


  },

]
new navigation(obj);


function getDate(){
  let time = new Date();
  return `${time.getFullYear()}-${time.getMonth()}-${time.getDate()} ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`
}



class searchResult {
     
  constructor(obj){
    this.books = $('.show_books')[0];
    this.detail = $('.detail')[0];
    this.obj = obj;
  
    
    this.render();
    this.model();
    this.borrow();

  }
  // getData(){
    remove(){
      $(this.books).empty();
      $(this.detail).empty();
    }
    
  // }
  borrow(){
      $("[class*='borrow']").click((e)=>{
      let index = $("[class*='borrow']").index(e.target);
      
      //  console.log(e.target.className.indexOf('no'));
      if(e.target.className.indexOf('no')!==-1){
        alert('这本书借完啦,过段时间再来吧！');
      } else { 
        $('header,footer,.search_logo,.show_books').addClass('blur')
        $('.book_model')[index].style.display='block';
        document.body.style.overflow = "hidden" 
      }
    })
    $('.detail_close').click((e)=>{
      let index = $("[class*='detail_close']").index(e.target);
      $('header,footer,.search_logo,.show_books').removeClass('blur')
      $('.book_model')[index].style.display='none';
      document.body.style.overflow = "" ;
    })
    $('.detail_btn').click((e)=>{
        // alert('haha')
      let index = $("[class*='detail_btn']").index(e.target);
    
        let borrowInfor={
        username:'zhangshuai2021005731',  //用户名
        bookid:'',
        // bookname:'',   //书名
        borrow_date:'',//借阅日期
        time:''//借阅时间
        
      }
      let fliter = /[\D]/;
      let duration = /[\d]{1,2}/;
      borrowInfor.username = localStorage.getItem('username');
      borrowInfor.time=$('.detail_duration')[index].value;
      if($('.detail_duration')[index].value==''){
        alert('请输入有效借阅天数');
        return
      }
        
      if(fliter.test(borrowInfor.time)=== true) {
        return alert('请输入有效借阅天数')
      }
       else if (borrowInfor.time.match(duration)[0]){
        let number=borrowInfor.time.match(duration)[0];
        if(number>0&&number<=30){ 
        borrowInfor.time = number;
        // console.log(borrowInfor.time);
        // console.log(10);
        }else{
        alert('请输入有效借阅时长');
        return;
      }
    }
      
      borrowInfor.borrow_date=getDate();

      borrowInfor.bookid=this.obj[index].book_id;
      // borrowInfor.bookid="1";
      console.log(borrowInfor)
    // console.log(borrowInfor.bookid)
         axiosget({
           menthod:'post',
           data:borrowInfor
         }).then(response=>{
          //  console.log(response);
            console.log(response.data.message)
         },reason=>{
            console.log(reason.data.message)
         })
    })

  }

  

   render(){

    
    for(let i=0;i<this.obj.length;i++){
     
    let book = document.createElement('div');
    book.className = 'show_book';
    //
    let img = document.createElement('img');
    img.src = this.obj[i].src;
   // 
    let filterName = '';
    let name = document.createElement('h4');
    name.className = 'book_name';
    name.title= this.obj[i].bookName ;
    filterName =`《 ${this.obj[i].bookName} 》`;
    if(this.obj[i].bookName.length>18){
      
       filterName ="《 "+ this.obj[i].bookName.slice(0,15)+'...》';
      
    }
    name.innerHTML = filterName;
    //
    let filterRmd = '';
    filterRmd = this.obj[i].recommend
    let recommend = document.createElement('p');
    recommend.className = 'book_recommend';
    recommend.title= this.obj[i].recommend;
    if(this.obj[i].recommend.length>42){
      filterRmd = this.obj[i].recommend.slice(0,40)+'...';
    }
    recommend.innerHTML = filterRmd;
    let borrow = document.createElement('div');
    borrow.innerHTML = this.obj[i].state;
   
    if(this.obj[i].state === '借书'){
     
      borrow.className = 'book_borrow';
    } else {
      borrow.className = 'book_noborrow';
    }
     
    book.insertAdjacentElement('beforeend',img)
    book.insertAdjacentElement('beforeend',name)
    book.insertAdjacentElement('beforeend',recommend)
    book.insertAdjacentElement('beforeend',borrow)
    this.books.insertAdjacentElement('beforeend',book);
    }
 
  }
  
  model(){
    
    for(let i=0;i<this.obj.length;i++){
      //
      let model = document.createElement('div');
      model.className = 'book_model';
      //
      let img = document.createElement('img');
       img.src= this.obj[i].src;
      //
      let bookDetail = document.createElement('div');
      bookDetail.className = 'book_detail';
      //
      let author = document.createElement('span');
      author.className = 'detail_author';
      author.innerHTML=`书籍作者: ${this.obj[i].author}`;

      let recommend = document.createElement('span');
      recommend.className = 'detail_recommend';
      if(this.obj[i].recommend.length>200){
        this.obj[i].recommend
      }
      recommend.innerHTML=`书籍推荐: ${this.obj[i].recommend}`;
      //
      let number = document.createElement('span');
      number.className = 'detail_number';
      number.innerHTML=`图书库存: ${this.obj[i].currrent_number}本 `;
      //
      let position = document.createElement('span');
      position.className = 'detail_position';
      position.innerHTML=`图书位置: ${this.obj[i].position}`;
      //
      let borrow = document.createElement('div');
      borrow.className = 'detail_btn';
      borrow.innerHTML='我要借书';
      //
      let close = document.createElement('span');
      close.className = 'detail_close';
      close.innerHTML = '×';
      //
      // setInterval(()=>{time.innerHTML = `${this.borrowTime}`},1000)
      let time = document.createElement('span');
      time.className ='detail_time';
      //
      let  duration = document.createElement('input');
      duration.className = 'detail_duration';
      duration.type='number';
      duration.max='30',duration.min='1';
      $(bookDetail).append(author);
      $(bookDetail).append(recommend);
      $(bookDetail).append(number);
      $(bookDetail).append(position);
      $(bookDetail).append(`借阅时间(1-30天):`);
      $(bookDetail).append(duration);
      $(bookDetail).append(borrow);
      $(model).append(img);
      $(model).append(bookDetail);
      $(model).append(close);
  
      model.style.display='none';
      $(this.detail).append(model);
    
    }
  

  }
  }



let axiospost = axios.create({   
  baseURL: 'http://zybbi2.natappfree.cc',
  timeout: 3000,
  method:'post'
});

   let post ={
   searchType: '',
   searchValue:'',
}
  let result = null;
  let  count =0;
  function btn_search(e,parm1,parm2){
    
   post.searchType = parm1??$('.search_type')[0].value;
   console.log(post.searchType)
   post.searchValue= parm2??$('.search_search')[0].value;
   

   if(!count){
    //  count++; 
   } else if(post.searchValue==''){
     alert("请输入相关信息");
     return
   }
   count++; 
    localStorage.removeItem('searchType');
    localStorage.removeItem('searchValue');

    axiospost({
     data:post,
     url:'/search'
    }).then(response=>{
      // console.log(count===1)
     if(count==1){
       console.log(count);
        console.log('hahah');
     }else if($.isEmptyObject(response.data)){
       alert('书院暂无此书');
       return ;
     }
     if(result!==null)
     {
       result.remove();
     }
     let obj2 = response.data; 
     console.log(obj2);
     result = new searchResult(obj2);
     
    },reason=>{
      alert('拒绝访问！');
    }).catch(error=>{
      alert(error);
    })
}
$('.btn_search')[0].onclick = btn_search;
window.onload=function(){ 
   let e=null
   let parm1,parm2;
   parm1 =localStorage.getItem('searchType');
   parm2 =localStorage.getItem('searchValue');
  //  parm1='allBook'
 
   btn_search(e,parm1,parm2);
  
} 

$('.toTop').click(()=>{
  window.scrollTo(0,0);
})

console.log($('.nav_user'));
$('.nav_user')[0].innerHTML=`${localStorage.getItem('username')??"您好"},欢迎进入云顶书库`;
